package com.uchengwang.ui.cm_ui.cycle.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.adapter.MsgNoticyAdapter;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleNoticeBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;

import java.util.List;

/**
 * Created by U城网 on 2018/2/5.
 * 消息通知界面
 */

public class MsgNoticyActivity extends BaseWithTitleActivity{
    private RecyclerView recycleView;
    private MsgNoticyAdapter adapter;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private RecyclerAdapterWithHF mAdapter;
    private int page = 1;//当前页  每页请求多少数据
    private int totlaPagers ;//总页数【服务器返回】

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ustudy_2);
        setTitle("互动通知");
        initView();
    }

    private void initView() {
        recycleView = (RecyclerView) findViewById(R.id.u_study_recycleView);
        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.mRefreshLayout);
        initData();
    }

    private void initData() {
        adapter = new MsgNoticyAdapter(this);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(mAdapter);

        mSwipeRefreshLayout.setAutoLoadMoreEnable(true);
        mSwipeRefreshLayout.setLoadMoreEnable(true);

        mSwipeRefreshLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                mSwipeRefreshLayout.loadMoreComplete(true);
                getData(true);
            }
        });

        mSwipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totlaPagers){
                    //当前请求页大于总页数，不再请求
                    getData(false);
                } else {
                    mSwipeRefreshLayout.loadMoreComplete(false);
                }
            }
        });

    }

    /**
     * @param isRefresh 是数据刷新还是加载更多 true 刷新
     * */
    private void getData(final boolean isRefresh){
        CycleHttpUtils.getUtils().getCycleNotice(this, page, 10, true, null, new OnHttpResultListener<CycleNoticeBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<CycleNoticeBean> list) {
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }

                totlaPagers = total;
                if (list != null){
                    if (isRefresh){
                        adapter.setList(list);
                    }else {
                        adapter.getList().addAll(list);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }
}
